<?php
/**
 * View Nuovo Post - LaFabbrica.org
 * 
 * @author     luigi.pastore (gigi@la-fabbrica.org)
 * @created    21-apr-2011
 * @package    LaFabbrica
 * @copyright  Copyright (c) 2011 Luigi Pastore
 */
?>
<script type="text/javascript" src="/js/ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="/js/ckeditor/adapters/jquery.js"></script>
<script type="text/javascript" src="/js/ckeditor/load-editor.js"></script>
<script type="text/javascript">
//<![CDATA[
    $(document).ready(function() {
	// Datepicker
	$( "#year" ).datepicker({
		// Riempie i campi della data...
		onSelect: function(dateText, inst) {
		    var theDate = new Date(Date.parse($(this).datepicker('getDate')));
		    var day = $.datepicker.formatDate('dd', theDate);
		    var month = $.datepicker.formatDate('mm', theDate);
		    var year = $.datepicker.formatDate('yy', theDate);
		    $('#day').val(day);
		    $('#month').val(month);
		    $('#year').val(year);
		},
		showOn: "button",
		showAnim: 'blind',
		buttonImage: "/images/admin/calendar.png",
		buttonImageOnly: true
	});

	$.validator.addMethod(
	    "regex",
	    function(value, element, regexp) {
		return regexp.test(value);
	    }
	);
	// a custom method for validating the date range
	$.validator.addMethod("validDate", function(value, element, classname) {
	    if($("#day").val() == '' && $("#month").val() == '' && $("#year").val() == '')
		return false;
	
	    try{$.datepicker.parseDate('ddmmyy', $("#day").val()+$("#month").val()+$("#year").val() );return true;}
		 catch(e){ console.log(e); return false;}

	}, "<?php echoERR_DATE?>");

	// a new class rule to group all three methods
	$.validator.addClassRules({
	    dateValid: {validDate:true}
	});
	
	$("#postForm").validate({
	    invalidHandler: function(e, validator) {
		var errors = validator.numberOfInvalids();
		if (errors) {
		    $("li#errorLi").show();
		} else {
		    $("li#errorLi").hide();
		};
	    },
	    
	    // Assegna classe 'error' al <li> dell'elemento invalido
	    errorPlacement: function(error, element) {
		//console.log(element);
		element.parent("span").parent("div").parent("li").addClass("error");
		error.appendTo( element.parent("span").parent("div"));
	    },
	    submitHandler: function(form) {
		$('#salva').attr('disabled', 'disabled');
		form.submit();
	    },	    
	    errorElement: "p",
	    groups: {
		dateValid: "day month year"
	      },
	    rules: {
		post_title:{
		    required: true
		},
		post_text:{
		    required: true
		},
		post_image:{
		    accept: "png|jpe?g|gif|bmp"
		}
	    },
	    messages: {
		post_title: {
		    required: "<?php echo ERR_EMPTY ?>"
		},
		post_text: {
		    required: "<?php echo ERR_EMPTY ?>"
		},
		post_image: {
		    accept: "<?php echo ERR_EXTENSION ?>"
		}
	    },
	    debug:true
	});

	// Risolve un bug d'integrazione tra jQuery e CKeditor
	$("#salva").click(function(){
			CKEDITOR.instances.post_text.updateElement();
		});

	$( '#post_text' ).ckeditor(function(){}, ckeditorConf );
	
	$('#post_text').val('<?php echo (addslashes($this->data['post_text']))?>');

    });
    
    $("#salva").click(function() {
      validator.form();
    });
    
    //]]>
</script>

<div class="info">
    <h2>Inserisci Nuovo Post</h2>
    <div>Per inserire un nuovo post...</div>
</div>
<!-- This <div> holds alert messages to be display in the sample page. -->
<div id="alerts">
    <noscript>
    <p>
	<strong>CKEditor requires JavaScript to run</strong>. In a browser with no JavaScript
				support, like yours, you should still see the contents (HTML data) and you should
				be able to edit it normally, without a rich editor interface.
    </p>
    </noscript>
</div>
<form class="fab" name="postForm" id="postForm" action="/admin/posts/edit" method="post" enctype="multipart/form-data">
    <ul>
	<li id="errorLi" style="<?php echo$this->errorMsg ? '' : 'display:none'?>">
	    <h3 id="errorMsgLbl">Si Ã¨ verificato un problema durante l'invio del modulo.</h3>
	    <p id="errorMsg">
		Di seguito trovi evidenziati gli errori riscontrati.
	    </p>
	</li>
	
	<li class="<?php echo$this->errorMsg['post_title'] ? 'error' : ''?>">
	    <label class="desc <?php echo$this->errorMsg['post_title'] ? 'error' : ''?>">Titolo<span class="req">*</span></label>
	    <div>
		<span>
		    <input id="post_title" name="post_title" class="field text large" type="text" maxlength="255" value="<?php echo$this->data['post_title']?>" /> 
		</span>
	    </div>
	    <p class="error" style="<?php echo$this->errorMsg['post_title'] ? 'display:block' : ''?>"><?php echo$this->errorMsg['post_title']?></p>
	    <p id="instruct1" class="instruct">
		<img src="/uploads/posts/post_<?php echo$this->data['post_id']?>.<?php echo$this->data['post_image_ext']?>" style="width:100%" alt="Immagine Principale" title="Immagine Principale" />
	    </p>	
	</li>
	
	<li class="<?php echo$this->errorMsg['post_text'] ? 'error' : ''?>">
	    <label class="desc <?php echo$this->errorMsg['post_text'] ? 'error' : ''?>">Testo<span class="req">*</span></label>
	    <div>
		<span>
		    <textarea id="post_text" name="post_text" rows="10" cols="50"><?//=  htmlentities($this->data['post_text'])?></textarea>
		</span>
	    </div>
	    <p class="error" style="<?php echo$this->errorMsg['post_text'] ? 'display:block' : ''?>"><?php echo$this->errorMsg['post_text']?></p>
	</li>
	
	<li class="section">
	    <h3>Altre Informazioni</h3>
	</li>
	
	<li class="<?php echo$this->errorMsg['post_published'] ? 'error' : ''?>">
	    <label class="desc <?php echo$this->errorMsg['post_published'] ? 'error' : ''?>">Data</label>
	    <div class="float-left">
		<span class="input">
		    <input class="field text dateValid" name="day" id="day" size="2" type="text" maxlength="2" value="<?php echo$this->data['day'] ? $this->data['day'] : date('d')?>" /> / 
		    <label>DD</label>
		</span>
		    
		<span class="input">
		    <input class="field text dateValid" name="month" id="month" size="2" type="text" maxlength="2" value="<?php echo$this->data['month'] ? $this->data['month'] :date('m')?>" /> / 
		    <label>MM</label>
		</span>
		    
		<span class="input">
		    <input class="field text dateValid" name="year" id="year" size="4" type="text" maxlength="4" value="<?php echo$this->data['year'] ? $this->data['year'] :date('Y')?>" />
		    <label>YYYY</label>
		</span>
		    
<!--		<span class="input">
		    <img alt="Pick a date." src="/images/admin/calendar.png" onclick="$('#datepicker').trigger('click');" class="datepicker" id="datepicker">
		</span>-->
	    </div>
	    <p class="error" style="<?php echo$this->errorMsg['post_published'] ? 'display:block' : ''?>"><?php echo $this->errorMsg['post_published'] ?></p>
	    <p id="instruct2" class="instruct">
		<small>Inserisci la data di pubblicazione del post.
		    <br/>Questo verrÃ  visualizzato online a partire dalla data inserita.</small>
	    </p> 
	</li>
	
	<li>
	    <label class="desc">Categoria</label>
	    <div>
		<?php foreach( $this->categories as $category ) : ?>
		    <span class="input">
			<input id="<?php echo$category['category_name']?>" name="post_category" class="field radio" type="radio" value="<?php echo$category['category_id']?>" checked="checked" />
			<label class="choice" for="<?php echo$category['category_name']?>"><?php echo$category['category_name']?></label>
		    </span>
		<?php endforeach; ?>
	    </div>
	</li>
	
	<li class="<?php echo$this->errorMsg['post_tags'] ? 'error' : ''?>">
	    <label class="desc <?php echo$this->errorMsg['post_tags'] ? 'error' : ''?>">Tags</label>
	    <div>
		<span>
		    <input id="post_tags" name="post_tags" class="field text large" type="text" maxlength="255" value="<?php echo$this->data['post_tags']?>" /> 
		</span>
	    </div>
	    <p class="error" style="<?php echo$this->errorMsg['post_tags'] ? 'display:block' : ''?>"><?php echo$this->errorMsg['post_tags']?></p>
	    <p id="instruct3" class="instruct">
		<small>Inserisci i tag separati da <strong>;</strong>.
		    <br/>Es: <strong>tag1</strong>; <strong>tag2</strong>;</small>
	    </p>
	</li>
	
	<li class="section">
	    <h3>&nbsp;</h3>
	</li>
	
	<li class="complex <?php echo$this->errorMsg['post_image'] ? 'error' : ''?>">
	    <label class="desc <?php echo$this->errorMsg['post_image'] ? 'error' : ''?>">Modifica Immagine Principale</label>
	    <div>
		<span class="input">
		    <input class="field file" type="file" name="post_image" id="post_image" value="" />
		</span>
	    </div>
	    <p class="error" style="<?php echo$this->errorMsg['post_image'] ? 'display:block' : ''?>"><?php echo $this->errorMsg['post_image'] ?></p>
	    <p id="instruct4" class="instruct">
		<small>I formati ammessi sono
		    <br/><strong>PNG</strong>, <strong>JPG</strong>, <strong>GIF</strong> e <strong>BMP</strong></small>
	    </p>	    
	</li>
	<input id="id" name="id" type="hidden" value="<?php echo$this->data['post_id']?>" />
    </ul>
    <div id="formButtons">
	<button id="salva" type="submit" name="salva" value="salva" class="button positive">
	    <img src="/images/admin/tick.png" alt="" />Salva
	</button>
    </div>
</form>
